package com.enqualcomm.kids.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserTerminalDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.SetWatchTimeParams;
import com.enqualcomm.kids.network.socket.response.BasicResult;
import com.enqualcomm.kids.network.socket.response.QueryUserTerminalInfoResult;
import com.enqualcomm.kids.network.socket.response.TerminallistResult;
import com.enqualcomm.kids.view.dialog.MyToolsTimePickerDialog;
import com.enqualcomm.kids.view.dialog.MyToolsTimerPickerDialog2;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import common.utils.PromptUtil;

public class SetWatchTimeActivity extends BaseActivity implements View.OnClickListener {

    private Button mSubmit_bt;
    private String mdateStr;
    private String mTimeStr;
    private TextView mDate_tv;
    private TextView mTime_tv;


    int yearInit = 0;
    int monthInit = 0;
    int dayInit = 0;

    int hour = 0;
    int minute = 0;
    private TerminallistResult.Terminal terminal;
    private NetworkModel networkModel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_watch_time);


        terminal = getTerminal();
        networkModel = new NetworkModel();
        initView();
    }

    @Override
    protected void onStop() {
        super.onStop();
        networkModel.onStop();
    }

    private void initView() {
        findViewById(R.id.title_bar_left_iv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        QueryUserTerminalInfoResult.Data info = new UserTerminalDefault(terminal.userterminalid).getInfo();
        setupTitleIcon(info,terminal.terminalid,terminal.userterminalid,getString(R.string.datetime));


        mSubmit_bt = (Button) findViewById(R.id.submit_bt);
        mDate_tv = (TextView) findViewById(R.id.date_tv);
        mTime_tv = (TextView) findViewById(R.id.time_tv);

        mDate_tv.setOnClickListener(this);
        mTime_tv.setOnClickListener(this);
        mSubmit_bt.setOnClickListener(this);

        mdateStr = getNowTime(0);
        mTimeStr = getNowTime(1);
        mTime_tv.setText(mTimeStr);
        mDate_tv.setText(mdateStr);
    }

    @Override
    public void onClick(View view) {
        int viewid = view.getId();
        switch (viewid) {
            case R.id.date_tv:
                showDateDialog();
                break;
            case R.id.time_tv:
                showTimeDialog();
                break;
            case R.id.submit_bt:
                sendMsgToServer();
                break;
        }
    }
    private void sendMsgToServer() {
        AppDefault appDefault = new AppDefault();
        String setTime = mdateStr + " " + mTimeStr;
        SetWatchTimeParams params = new SetWatchTimeParams(appDefault.getUserkey(),appDefault.getUserid()
                , terminal.terminalid, setTime);
        networkModel.loadDataFromServer(new SocketRequest<BasicResult>(params, new NetworkListener<BasicResult>() {
            @Override
            public void onError(VolleyError error) {
                PromptUtil.toast(getApplicationContext(), "VolleyError设置时间成功");
            }

            @Override
            public void onSuccess(BasicResult response) {
                if (response.code == 0) {
                    PromptUtil.toast(getApplicationContext(), getString(R.string.main_operate_success));
                } else {
                    PromptUtil.toast(getApplicationContext(), getString(R.string.failed));
                }
            }
        }));
    }

    private void showTimeDialog() {

        if (TextUtils.isEmpty(mTimeStr)) {
            Calendar calendar = Calendar.getInstance();
            hour = calendar.get(Calendar.HOUR_OF_DAY);
            minute = calendar.get(Calendar.MINUTE);
        } else {
            String[] split = mTimeStr.split(":");
            hour = Integer.parseInt(split[0]);
            minute = Integer.parseInt(split[1]);
        }

        new MyToolsTimerPickerDialog2(this, new MyToolsTimerPickerDialog2.OnDateTimeSetListener() {
            @Override
            public void onDateTimeSet(int year, int monthOfYear, int dayOfMonth, int hour, int minute) {
                String strHour = "";
                if (hour < 10)
                    strHour = "0" + hour;
                else
                    strHour = "" + hour;

                String strminute = "";
                if (minute < 10)
                    strminute = "0" + minute;
                else
                    strminute = "" + minute;
                mTimeStr = strHour + ":" + strminute;
                mTime_tv.setText(mTimeStr);
            }
        }, hour, minute).show();

    }

    private void showDateDialog() {

        if (TextUtils.isEmpty(mdateStr)) {
            Calendar calendar = Calendar.getInstance();
            yearInit = calendar.get(Calendar.YEAR);
            monthInit = calendar.get(Calendar.MONTH);
            dayInit = calendar.get(Calendar.DAY_OF_MONTH);
        } else {
            String[] split = mdateStr.split("-");
            yearInit = Integer.parseInt(split[0]);
            monthInit = Integer.parseInt(split[1]) - 1;
            dayInit = Integer.parseInt(split[2]);
        }

        new MyToolsTimePickerDialog(this, new MyToolsTimePickerDialog.OnDateTimeSetListener() {
            @Override
            public void onDateTimeSet(int year, int month, int dayOfMonth,
                                      int hour, int minute) {
                String monthStr = "";
                if (month < 10) {
                    monthStr = "0" + month;
                } else {
                    monthStr = String.valueOf(month);
                }
                String dayStr = "";
                if (dayOfMonth < 10) {
                    dayStr = "0" + dayOfMonth;
                } else {
                    dayStr = String.valueOf(dayOfMonth);
                }
                mdateStr = year + "-" + monthStr + "-" + dayStr;
                mDate_tv.setText(mdateStr);
            }
        }, yearInit, monthInit, dayInit).show();
    }

    /**将当前时间转换为制定格式*/
    private String getNowTime(int dayOrtime){
        String retStr="";
        if (dayOrtime == 0)
        {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            retStr = dateFormat.format(new Date());
        } else if(dayOrtime ==1) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
            retStr = dateFormat.format(new Date());
        }
        return retStr;
    }




}
